package com.ldatum.crm.action.admin;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.ldatum.crm.domain.TDepartment;
import com.ldatum.crm.domain.TEmployee;
import com.ldatum.crm.domain.TPosition;
import com.ldatum.crm.service.TDepartmentService;
import com.ldatum.crm.service.TEmployeeService;
import com.ldatum.crm.service.TPositionService;
import com.ldatum.crm.serviceImpl.TDepartmentServiceImpl;
import com.ldatum.crm.serviceImpl.TEmployeeServiceImpl;
import com.ldatum.crm.serviceImpl.TPositionServiceImpl;

@ParentPackage("admin")
@Results({ 
	@Result(name = "mainAction", location = "admin!main.action",type = "redirect")
})
public class PositionAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TEmployee employee = new TEmployee();
	
	static TEmployeeService employeeService = new TEmployeeServiceImpl();
	static TDepartmentService departmentService = new TDepartmentServiceImpl();
	static TPositionService positionService = new TPositionServiceImpl();
	
	List<TEmployee> employeeList = new ArrayList<TEmployee>();
	List<TDepartment> departmentList = new ArrayList<TDepartment>();
	List<TPosition> positionList = new ArrayList<TPosition>();
	
	private String levelOne;
	private String levelTwo;
	
	private int departId;
	
	public String list() throws Exception{
		positionList = positionService.list(TPosition.class);
		levelOne = "employee";
		levelTwo = "employeeList";
		return "listEmployee";
	}
	
	public String add() throws Exception{
		departmentList = departmentService.list();
		employee.setLoginname("");
		employee.setPassword("");
		employee.setStatus("在职");
		TDepartment depart = new TDepartment();
		depart.setId(0);
		employee.setDepartment(depart);
		TPosition po = new TPosition();
		po.setId(0);
		employee.setPosition(po);
		levelOne = "employee";
		levelTwo = "employeeAdd";
		return "addEmployee";
	}
	
	public void findByDepartId() throws Exception{
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			positionList = positionService.findByDepartId(departId);
			for(TPosition position:positionList){
				position.setDepartment(null);
			}
			json.accumulate("positionList",positionList);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	public TEmployee getEmployee() {
		return employee;
	}

	public void setEmployee(TEmployee employee) {
		this.employee = employee;
	}

	public List<TEmployee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<TEmployee> employeeList) {
		this.employeeList = employeeList;
	}

	public String getLevelOne() {
		return levelOne;
	}

	public void setLevelOne(String levelOne) {
		this.levelOne = levelOne;
	}

	public String getLevelTwo() {
		return levelTwo;
	}

	public void setLevelTwo(String levelTwo) {
		this.levelTwo = levelTwo;
	}

	public List<TDepartment> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<TDepartment> departmentList) {
		this.departmentList = departmentList;
	}

	public List<TPosition> getPositionList() {
		return positionList;
	}

	public void setPositionList(List<TPosition> positionList) {
		this.positionList = positionList;
	}

	public int getDepartId() {
		return departId;
	}

	public void setDepartId(int departId) {
		this.departId = departId;
	}
	
}
